public class MyBlockingQueue<T> {
    private Object[] elem;
    private int size = 0; //当前元素个数
    private int capability = 10; //当前容量
    private int head = 0; //队头位置
    private int tail = 0; //队尾位置

    public MyBlockingQueue() {
        elem = new Object[capability];
    }

    public MyBlockingQueue(int capability) {
        this.capability = capability;
        elem = new Object[this.capability];
    }

    public synchronized void put(T value) throws InterruptedException {
        while(capability == size) {
            wait();
        }
        size++;
        elem[tail++] = value;
        if(tail == capability) {
            tail = 0;
        }
        notify();
    }

    public synchronized T take() throws InterruptedException {
        while(size == 0) {
            wait();
        }
        size--;
        T ret = (T)elem[head++];
        if(head == capability) {
            head = 0;
        }
        notify();
        return ret;
    }
}
